package com.example.wendao.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wendao.R;
import com.example.wendao.model.entity.Goods;

import java.util.List;

public  class MallAdapter extends RecyclerView.Adapter<MallAdapter.ViewHolder> {
    private List<Goods> mgoodsList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView goodsImage;
        TextView goodsName;
        TextView goodsInfo;

        public ViewHolder(View view) {
            super(view);
            goodsImage = (ImageView) view.findViewById(R.id.goodspic);
            goodsName = (TextView) view.findViewById(R.id.goodsname);
            goodsInfo = (TextView) view.findViewById(R.id.goodinfo);

        }

    }

    public MallAdapter(List<Goods> goodsList) {
        mgoodsList = goodsList;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_mall_list, parent, false);
        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Goods goods = mgoodsList.get(position);
        if (position % 2 == 0) {

            holder.goodsImage.setImageResource(R.drawable.img4);

        } else {
            holder.goodsImage.setImageResource(R.drawable.img2);

        }
        holder.goodsName.setText(goods.getGoodName());
        holder.goodsInfo.setText(goods.getGoodInfo());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(),"即将跳转", Toast.LENGTH_SHORT).show();
                Navigation.findNavController(view).navigate(R.id.action_fragment_mall_to_fragment_mall_details);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mgoodsList.size();
    }



}
